<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2005/12/16 20:58:31 $)
// $RCSfile: cron_cleanup.php,v $ : $Revision: 1.10 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

global $cron;

/**
* ###############################################
* HOURLY CLEANUP
* ###############################################
*/
if ($eventaction == "hourlycleanup")
{

	$cron->logentry($_SWIFT["cronid"], "success");

	// Clear all sessions which have been inactive
	$cleartime = DATENOW - $_SWIFT["settings"]["security_sessioninactivity"];
	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."sessions` WHERE `lastactivity` < '". $dbCore->escape($cleartime) ."';");

	processMailQueue();

/**
* ###############################################
* DAILY CLEANUP
* ###############################################
*/
} else if ($eventaction == "dailycleanup") {
	$cron->logentry($_SWIFT["cronid"], "success");
	
	// Cleanup logs
	$cleartime = DATENOW - (intval($_SWIFT["settings"]["cpu_logcleardays"])*86400);
	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."cronlogs` WHERE `dateline` < '". $dbCore->escape($cleartime) ."';");
	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."userverifyhash` WHERE `dateline` < '". $dbCore->escape($cleartime) ."';");
	if ($_SWIFT["settings"]["user_adelunveri"] == 1)
	{
		$usercleartime = DATENOW - (intval($_SWIFT["settings"]["user_delcleardays"])*86400);

		$_useridlist = array();
		$dbCore->query("SELECT `userid` FROM `". TABLE_PREFIX ."users` WHERE `enabled` = '0'  AND `dateline` < '". $dbCore->escape($usercleartime) ."';");
		while ($dbCore->nextRecord())
		{
			$_useridlist[] = $dbCore->Record["userid"];
		}

		$dbCore->query("DELETE FROM `". TABLE_PREFIX ."users` WHERE `userid` IN (". buildIN($_useridlist) .");");
		$dbCore->query("DELETE FROM `". TABLE_PREFIX ."useremails` WHERE `userid` IN (". buildIN($_useridlist) .");");
	}
	
	$tempfileidlist = array();
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."tempfiles` WHERE `dateline` < '". $dbCore->escape($cleartime) ."';");
	while ($dbCore->nextRecord())
	{
		unlink("./files/".$dbCore->Record["filename"]);
		$tempfileidlist[] = $dbCore->Record["tempfileid"];
	}
	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."tempfiles` WHERE `tempfileid` IN (". buildIN($tempfileidlist) .");");

	if ($module->isRegistered(MODULE_PARSER))
	{
		$_parserlogidlist = array();
		$dbCore->query("SELECT `parserlogid` FROM `". TABLE_PREFIX ."parserlogs` WHERE `dateline` < '". intval($cleartime) ."';");
		while ($dbCore->nextRecord())
		{
			$_parserlogidlist[] = $dbCore->Record["parserlogid"];
		}

		if (count($_parserlogidlist))
		{
			$dbCore->query("DELETE FROM `". TABLE_PREFIX ."parserlogs` WHERE `parserlogid` IN (". buildIN($_parserlogidlist) .");");
			$dbCore->query("DELETE FROM `". TABLE_PREFIX ."parserlogdata` WHERE `parserlogid` IN (". buildIN($_parserlogidlist) .");");
		}

	}
}

?>